package org.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Test1 {
    //leetcode 39 组合总和 https://leetcode.cn/problems/combination-sum/
    public List<List<Integer>> ret;
    List<Integer> list;
    public int sum;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ret = new ArrayList<>();
        list = new ArrayList<>();
        backtracking(candidates,target,0);
        return ret;
    }

    public void backtracking(int[] candidates,int target,int start) {
        if (sum > target) return;
        if (sum == target) {
            ret.add(new ArrayList<>(list));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            sum += candidates[i];
            list.add(candidates[i]);
            backtracking(candidates,target,i);
            sum -= candidates[i];
            list.remove(list.size() - 1);
        }
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        int[] arr = {2,3,6,7};
        test1.combinationSum(arr,7);
        System.out.println(999);
    }
}
